/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 *
 * @data:2017年10月17日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.service.transaction;

import com.wtkj.vo.dzzf.base.kfwd.TransIn;
import com.wtkj.vo.dzzf.base.kfwd.WriteOffTransIn;
import com.wttech.dzzf.process.dao.ReverSalInfoDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.transaction.Recharge;
import com.wttech.dzzf.process.model.transaction.ReverSal;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 冲正交易服务实现
 *
 * @author MaShibo
 * @date 2017年10月17日
 */
@Service
public class ReverSalInfosCreatorService {


    private static final Logger LOGGER = LoggerFactory.getLogger(ReverSalInfosCreatorService.class);


    @Resource
    private RechargeInfosCreatorService rechargeInfosCreatorService;

    @Resource
    private UserDetailInfosCreatorService userDetailInfosCreatorService;

    @Resource
    private ReverSalInfoDao reverSalInfoDao;

    @Resource
    private TransferUserInfoDao transferUserInfoDao;

    @Resource
    private ThreadPoolTaskExecutor executor;


    /**
     * 创建冲正交易
     */
    private void createReverSalInfos() {
        List<WriteOffTransIn> offTransIns = reverSalInfoDao.queryWriteOffTransinInfos();
        int total = offTransIns.size();
        LOGGER.info("共{}条冲正数据", total);
        List<Future<Void>> futures = new ArrayList<>(offTransIns.size());
        offTransIns.forEach(offTransIn -> futures.add(executor.submit(() -> {
            createReverSalInfo(offTransIn);
            return null;
        })));
        userDetailInfosCreatorService.getFutureResult(futures);
        LOGGER.info("冲正数据处理完成");
        executor.shutdown();
    }


    private void createReverSalInfo(WriteOffTransIn offTransIn) {

        //查询冲正交易对应的充值记录
        TransIn transIn = reverSalInfoDao.queryTransinInfoByTradeNo(offTransIn.getOldTradeNo());
        if (transIn == null) {
            LOGGER.warn("交易流水号【{}】的冲正交易无对应充值记录", offTransIn.getOldTradeNo());
            return;
        }

        //构建并处理充值记录相关信息
        Recharge recharge = rechargeInfosCreatorService.processSingleTransin(transIn);

        //构建并创建冲正记录信息
        ReverSal rs = new ReverSal();
        rs.setId(recharge.getId() + "1");
        rs.setCardId(recharge.getCardId());
        rs.setEffectiveTime(
                DateFormatUtils.ISO_DATETIME_FORMAT.format(offTransIn.getOptOn() == null ? new Date() : offTransIn.getOptOn()));
        try {
            transferUserInfoDao.createTestObjcet(rs);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }


    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ReverSalInfosCreatorService service = context.getBean(ReverSalInfosCreatorService.class);
        service.createReverSalInfos();
    }
}
